/*******************************************************************************
 * ATE, Automation Test Engine
 *
 * Copyright 2014, Montreal PROT, or individual contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Montreal PROT.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

// TODO: Auto-generated Javadoc
/**
 * This class PFLoginForm defines ....
 * @author Peidong Hu
 *
 */
public class PFLoginForm {
	
	/** The driver. */
	private final WebDriver driver;
	
	/** The login form we. */
	@FindBy(how = How.ID, using = "form-login")
	private WebElement loginFormWE;
	
	/** The user name we. */
	@FindBy(id = "modlgn_username")
	private WebElement userNameWE;
	
	/** The pass word we. */
	@FindBy(id = "modlgn_passwd")
	private WebElement passWordWE;
	
	/**
	 * @return the loginFormWE
	 */
	public WebElement getLoginFormWE() {
		return loginFormWE;
	}

	/**
	 * @param loginFormWE the loginFormWE to set
	 */
	public void setLoginFormWE(final WebElement loginFormWE) {
		this.loginFormWE = loginFormWE;
	}

	/**
	 * @return the userNameWE
	 */
	public WebElement getUserNameWE() {
		return userNameWE;
	}

	/**
	 * @param userNameWE the userNameWE to set
	 */
	public void setUserNameWE(final WebElement userNameWE) {
		this.userNameWE = userNameWE;
	}

	/**
	 * @return the passWordWE
	 */
	public WebElement getPassWordWE() {
		return passWordWE;
	}

	/**
	 * @param passWordWE the passWordWE to set
	 */
	public void setPassWordWE(final WebElement passWordWE) {
		this.passWordWE = passWordWE;
	}

	/**
	 * @return the loginSubmitWE
	 */
	public WebElement getLoginSubmitWE() {
		return loginSubmitWE;
	}

	/**
	 * @param loginSubmitWE the loginSubmitWE to set
	 */
	public void setLoginSubmitWE(final WebElement loginSubmitWE) {
		this.loginSubmitWE = loginSubmitWE;
	}

	/**
	 * @return the driver
	 */
	public WebDriver getDriver() {
		return driver;
	}

	/** The login submit we. */
	@FindBy(name = "Submit")
	private WebElement loginSubmitWE;
	
    /**
     * Instantiates a new PF login form.
     *
     * @param driver the driver
     */
    public PFLoginForm(final WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
        if (!loginFormWE.isDisplayed()) {
            throw new IllegalStateException("There is no login form");
        }
    }
    
    /**
     * Type username.
     *
     * @param username the username
     * @return the PF login form
     */
    public PFLoginForm typeUsername(final String username) {
        
    	userNameWE.sendKeys(username);

        return this;    
    }
    
    /**
     * Type password.
     *
     * @param password the password
     * @return the PF login form
     */
    public PFLoginForm typePassword(final String password) {
        
    	passWordWE.sendKeys(password);
        return this;    
    }
    
    /**
     * Submit login.
     *
     * @return the success login page
     */
    public SuccessLoginPage submitLogin() {
        
    	loginSubmitWE.submit();
        return new SuccessLoginPage(driver);    
    }
    
    /**
     * Submit false login.
     *
     * @return the false login page
     */
    public FalseLoginPage submitFalseLogin() {
       
    	loginSubmitWE.submit();
        return new FalseLoginPage(driver);   
    }

    /**
     * Login as.
     *
     * @param username the username
     * @param password the password
     * @return the success login page
     */
    public SuccessLoginPage loginAs(final String username, final String password) {
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }

}
